package com.li.util;

import com.li.pojo.AreaManage;
import com.li.pojo.BuildingManage;
import com.li.pojo.Host;
import com.li.pojo.RoomNumber;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Component;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: zhiqingLi
 * @Description: excel 读取
 * @Date: Created in 23:54 2024/3/13
 **/
public class ExcelPoi {
    //页面上传 进行读取 往数据库里添加
    public static <T> List<T> read(InputStream inputStream, Integer id) throws Exception {
        List<Object> dataList = null;
        XSSFWorkbook excel = new XSSFWorkbook(inputStream);
        switch (id) {
            case 1:
                dataList = new ArrayList<>();
                XSSFSheet sheetAt = excel.getSheetAt(0);
                int lastRowNum = sheetAt.getLastRowNum(); //最后一行
                for (int i = 2; i <= lastRowNum; i++) {
                    AreaManage areaManage = new AreaManage();
                    //获取一行
                    XSSFRow row = sheetAt.getRow(i);
                    areaManage.setAreaName(row.getCell(0).getStringCellValue());
                    areaManage.setAreaAddress(row.getCell(1).getStringCellValue());
                    areaManage.setPowerBill(row.getCell(2).getNumericCellValue());
                    areaManage.setInfo(row.getCell(3).getStringCellValue());
                    dataList.add(areaManage);
                }
                break;
            case 2:
                dataList = new ArrayList<>();
                XSSFSheet sheetAt1 = excel.getSheetAt(1);
                int lastRowNum1 = sheetAt1.getLastRowNum(); //最后一行
                for (int i = 2; i <= lastRowNum1; i++) {
                    BuildingManage buildingManage = new BuildingManage();
                    //获取一行
                    XSSFRow row = sheetAt1.getRow(i);
                    buildingManage.setAreaName(row.getCell(0).getStringCellValue());
                    buildingManage.setBuildingNum(Integer.parseInt(row.getCell(1).getStringCellValue()));
                    buildingManage.setBuildingName(row.getCell(2).getStringCellValue());
                    buildingManage.setUnitsNum(Integer.parseInt(row.getCell(3).getStringCellValue()));
                    buildingManage.setBuildingN(Integer.parseInt(row.getCell(4).getStringCellValue()));
                    buildingManage.setRoomNum(Integer.parseInt(row.getCell(5).getStringCellValue()));
                    buildingManage.setInfo(row.getCell(6).getStringCellValue());
                    dataList.add(buildingManage);
                }
                break;
            case 3:
                dataList = new ArrayList<>();
                XSSFSheet sheetAt2 = excel.getSheetAt(2);
                int lastRowNum2 = sheetAt2.getLastRowNum(); //最后一行
                for (int i = 2; i <= lastRowNum2; i++) {
                    RoomNumber roomNumber = new RoomNumber();
                    //获取一行
                    XSSFRow row = sheetAt2.getRow(i);
                    roomNumber.setAreaName(row.getCell(0).getStringCellValue());
                    roomNumber.setAreaN(Integer.parseInt(row.getCell(1).getStringCellValue()));
                    roomNumber.setAreaU(Integer.parseInt(row.getCell(2).getStringCellValue()));
                    roomNumber.setAreaM(Integer.parseInt(row.getCell(3).getStringCellValue()));
                    roomNumber.setAreaB(Integer.parseInt(row.getCell(4).getStringCellValue()));
                    roomNumber.setHostName(row.getCell(5).getStringCellValue());
                    roomNumber.setPhone(row.getCell(6).getStringCellValue());
                    roomNumber.setHostId(row.getCell(7).getStringCellValue());
                    roomNumber.setDirection(row.getCell(8).getStringCellValue());
                    roomNumber.setApartment(row.getCell(9).getStringCellValue());
                    roomNumber.setArea(row.getCell(10).getStringCellValue());
                    roomNumber.setTypeName(row.getCell(11).getStringCellValue());
                    roomNumber.setNature(row.getCell(12).getStringCellValue());
                    roomNumber.setIsf(row.getCell(13).getStringCellValue().charAt(0));
                    roomNumber.setHouseType(row.getCell(14).getStringCellValue());
                    roomNumber.setHouseMoney(row.getCell(15).getNumericCellValue());
                    roomNumber.setCharge(row.getCell(16).getStringCellValue());
                    roomNumber.setChargeNum(row.getCell(17).getNumericCellValue());
                    roomNumber.setWaterMoney(row.getCell(18).getNumericCellValue());
                    roomNumber.setPowerMoney(row.getCell(19).getNumericCellValue());
                    roomNumber.setWasteMoney(row.getCell(20).getNumericCellValue());
                    roomNumber.setInfo(row.getCell(21).getStringCellValue());
                    dataList.add(roomNumber);
                }
                break;
            case 4:
                dataList = new ArrayList<>();
                XSSFSheet sheetAt3 = excel.getSheetAt(3);
                int lastRowNum3 = sheetAt3.getLastRowNum(); //最后一行
                for (int i = 2; i <= lastRowNum3; i++) {
                    Host host = new Host();
                    //获取一行
                    XSSFRow row = sheetAt3.getRow(i);
                    host.setHostName(row.getCell(0).getStringCellValue());
                    host.setHostId(row.getCell(1).getStringCellValue());
                    host.setPhone(row.getCell(2).getStringCellValue());
                    host.setInfo(row.getCell(3).getStringCellValue());
                    dataList.add(host);
                }
                break;
        }
        return (List<T>) dataList;
    }
}
